package com.android.meige.view.group;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.android.meige.R;
import com.android.meige.activity.BaseObject;
import com.android.meige.pojo.BannerUintPojo;
import com.android.meige.utils.ConstantsUrl;
import com.android.meige.utils.JumpUtils;
import com.android.meige.utils.ObjectUtils;


public class BannerItemFragment extends Fragment {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private static final String ARG_PARAM3 = "param3";
    private String mParam1;
    private int mParam2;
    private String mParam3;
    private View view;
    private String topicId = "";

    public static BannerItemFragment newInstance(String thumb, int photolength, String link) {
        BannerItemFragment fragment = new BannerItemFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, thumb);
        args.putInt(ARG_PARAM2, photolength);
        args.putString(ARG_PARAM3, link);
        fragment.setArguments(args);
        return fragment;
    }

    public BannerItemFragment() {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getInt(ARG_PARAM2);
            mParam3 = getArguments().getString(ARG_PARAM3);
            if(mParam3!=null){
                String[] split = mParam3.split(":");
                if(split!=null && split.length>1){
                    topicId = split[1];
                }
            }
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (view != null) {
            ViewGroup parent = (ViewGroup) view.getParent();
            if (parent != null) {
                parent.removeView(view);
            }
        }
         view = inflater.inflate(R.layout.item_fragment_banner, null);
        ImageView iv = (ImageView) view.findViewById(R.id.imageView1);
        ObjectUtils.photo2(getActivity(), mParam1, iv);
        iv.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if(!TextUtils.isEmpty(mParam3)){
                    BannerUintPojo Apojo  = new BannerUintPojo();
                    Apojo.return_type = mParam3;
                    if(JumpUtils.jumpToEveryWhere(getActivity(), Apojo, BannerItemFragment.this))return;
                }
                if(!TextUtils.isEmpty(topicId)){
                    Intent intent = new Intent(getActivity(), BaseObject.class);
                    intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_TITLE, "专题活动");
                    intent.putExtra("topicname","专题活动");
                    intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_NEED_ID, topicId);
                    startActivity(intent);
                }
            }
        });

        return view;
    }



}
